#include "devapi.h"
#include "txn_par.h"

int get_keys( int secs )
{
    return kb_get_key( secs * 1000 );
}

static uint32_t get_rtc_seconds(void)
{
	struct timeval now;
	
	gettimeofday(&now, NULL);
	return now.tv_sec;
}

static uint32_t start_secs;		//..? api?
void timeouts_start( uint32_t start_time )
{	
	if( start_time != 0 )
		start_secs = start_time;
	else
		start_secs = get_rtc_seconds();
}

int timeouts_test( uint32_t timeout  )
{	
	uint32_t now = get_rtc_seconds( );
	if( now - start_secs > timeout )
		return 1;

	return 0;
}

int get_currtime()
{	
	return get_rtc_seconds();
}

int get_time(uint8_t * buf)
{
	time_t tv;
    struct tm t;
	time(&tv);
    localtime_r(&tv, &t);
	sprintf(buf, "%04d%02d%02d%02d%02d%02d",\
                   t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,\
                   t.tm_hour, t.tm_min, t.tm_sec);

}
int timeouts_cmp(uint32_t currtime,uint32_t timeout  )
{	
	uint32_t now = get_rtc_seconds( );
	if( now - currtime > timeout )
		return 1;

	return 0;
}

int _nv_write(uint32_t offset, uint8_t * buf, int buflen){

	int fd;
	fd = open(PARA_PATH,O_RDWR|O_CREAT);
	lseek( fd, offset, SEEK_SET);
	write( fd, buf,buflen);
	close(fd);
	return 0;

}

int _nv_read(uint32_t offset, uint8_t * buf, int buflen){

	int fd;
	fd = open(PARA_PATH,O_RDWR|O_CREAT);
	lseek( fd, offset, SEEK_SET);
	read( fd, buf,buflen);
	close(fd);
	return 0;
}


